package com.shujia.spark

import org.apache.spark.broadcast.Broadcast
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo19Bro {
  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf()
      .setMaster("local")
      .setAppName("map")


    val sc = new SparkContext(conf)


    val student: RDD[String] = sc.textFile("spark/data/students.txt")


    /*  val ids = List("1500100009", "1500100005", "1500100007")


      val filterRDD: RDD[String] = student.filter(line => {
        val id: String = line.split(",")(0)
        ids.contains(id)
      })*/


    //filterRDD.foreach(println)


    /**
      * 广播变量
      *
      */


    val ids = List("1500100009", "1500100005", "1500100007")

    //在Driver将一个变量广播
    val broad: Broadcast[List[String]] = sc.broadcast(ids)

    val filterRDD: RDD[String] = student.filter(line => {
      val id: String = line.split(",")(0)

      //在算子内获取广播变量中的数据
      val broadValue: List[String] = broad.value

      broadValue.contains(id)
    })

    filterRDD.foreach(println)

    /**
      * 使用限制
      * 1、广播变量只能在算子外面定义，只能在算子里面使用
      *
      */

  }

}
